# -*- encoding=utf8 -*-
import json,re
class task(object):
	"""docstring for tasklist"""
	def __init__(self):
		self.task_news = {"code":10000}
		self.task_video = {"code":10000}
		self.task_smallvideo = {"code":10000}
		self.task_official = {"code":10000}
		self.task_duration = {"code":10000}
		self.task_finish = {"code":10000}
		self.id_tasklist = 0
		self.id_finish = 0
		self.low_read_task = {"code":10000}
	'''
	获取任务完成数据
	'''
	def finish(self):
		self.task_finish = {"code":10000}
		file = "./interface/response/finish.json"
		with open(file,'rb') as f:
			re_json = json.load(f)
		try:
			time = re_json["id"]
			if re_json["subCode"] == 10000:
				res = re_json["data"]
			else:
				self.task_finish["code"] = re_json["subCode"]
		except Exception as e:
			self.task_finish = {"code":4,"msg":"任务异常"}
			return True
		else:
			if int(time) == int(self.id_finish):
				self.task_news = {"code":5,"msg":"任务未完成"}
				return True
			else:
				self.id_finish = time
				self.task_finish["code"] = 0
				self.task_finish["taskId"] = res["taskId"]
				self.task_finish["goldCoin"] = res["goldCoin"]
				self.task_finish["maxTimes"] = res["maxTimes"]
				self.task_finish["finishedTimes"] = res["finishedTimes"]
				self.task_finish["isFinished"] = res["isFinished"]
				if self.task_finish["taskId"] == 7 and self.task_finish["isFinished"] == 0:
					self.task_finish["isNextSurprise"] = res["isNextSurprise"]
					self.task_finish["nextTaskRolls"] = res["nextTaskRolls"]
					self.task_finish["alertTips"] = self.remove_h5_label(res["alertTips"])
					self.task_finish["alertButtonTips"] = res["alertButtonTips"]
					self.task_finish["officialRedPacketTips"] = res["officialRedPacketTips"]
				try:
					self.task_finish["duration_goldcoin"] = res["sumReward"]["goldCoin"]
					self.task_finish["duration_alertTips"] = res["sumReward"]["alertTips"]
					self.task_finish["duration_alertButtonTips"] = res["sumReward"]["alertButtonTips"]
				except:
					pass
				return True
	'''
	去除html标签
	'''
	def remove_h5_label(self,html):
		dr = re.compile(r'<[^>]+>',re.S)
		res = dr.sub('',html)
		return res
	'''
	获取任务列表下发数据
	'''	
	def get_tasklist(self):
		file = "./interface/response/tasklist.json"
		with open(file,'rb') as f:
			re_json = json.load(f)
		try:
			time = re_json["id"]
			tasks = re_json["data"]["taskList"]
		except Exception as e:
			self.task_video=self.task_official= self.task_smallvideo = self.task_duration = self.task_news= {"code":2}
			return True
		else:
			if int(time) == int(self.id_tasklist):
				self.task_video=self.task_official= self.task_smallvideo = self.task_duration = self.task_news= {"code":2}
				return True
			self.id_tasklist = time
			for task in tasks:
				if task["taskId"] == 1:
					if task["isFinished"] == 1:
						self.task_news = {"code":3}
					else:
						self.task_news["code"] = 0
						self.task_news["maxTimes"] = task["maxTimes"]
						self.task_news["isFinished"] = task["isFinished"]
						self.task_news["finishedTimes"] = task["finishedTimes"]
						self.task_news["sn_maxTimes"] = task["rules"]["maxTimes"]
						self.task_news["readTime"] = task["rules"]["readTime"]
						self.task_news["scrollTime"] = task["rules"]["scrollTime"]

				elif task["taskId"] == 3:
					if task["isFinished"] == 1:
						self.task_video = {"code":3}
					else:
						self.task_video["code"] = 0
						self.task_video["maxTimes"] = task["maxTimes"]
						self.task_video["isFinished"] = task["isFinished"]
						self.task_video["finishedTimes"] = task["finishedTimes"]
						self.task_video["sn_maxTimes"] = task["rules"]["maxTimes"]
						self.task_video["readTime"] = task["rules"]["readTime"]
				elif task["taskId"] == 8:
					self.low_read_task["code"] = 0
					self.low_read_task["finishedTimes"] = task["finishedTimes"]

				elif task["taskId"] == 6:
					if task["isFinished"] == 1:
						self.task_smallvideo = {"code":3}
					else:
						self.task_smallvideo["code"] = 0
						self.task_smallvideo["maxTimes"] = task["maxTimes"]
						self.task_smallvideo["isFinished"] = task["isFinished"]
						self.task_smallvideo["finishedTimes"] = task["finishedTimes"]
						self.task_smallvideo["sn_maxTimes"] = task["rules"]["maxTimes"]
						self.task_smallvideo["readTime"] = task["rules"]["readTime"]
				elif task["taskId"] == 7:
					if task["isFinished"] == 1:
						self.task_official = {"code":3}
					else:
						self.task_official["code"] = 0
						self.task_official["maxTimes"] = task["maxTimes"]
						self.task_official["isFinished"] = task["isFinished"]
						self.task_official["finishedTimes"] = task["finishedTimes"]
						self.task_official["scroll"] = int(task["rules"]["nextTaskRolls"]) - int(task["rules"]["scrolledRolls"])
						self.task_official["officialRedPacketTips"] = task["rules"]["officialRedPacketTips"]
						self.task_official["alertTips"] = self.remove_h5_label(task["rules"]["alertTips"])
						self.task_official["alertButtonTips"] = task["rules"]["alertButtonTips"]
						self.task_official["isNextTaskSurprise"] = task["rules"]["isNextTaskSurprise"]
				elif task["taskId"] == 2:
					if task["isFinished"] == 1:
						self.task_duration = {"code":3}
					else:
						self.task_duration["code"] = 0
						self.task_duration["maxTimes"] = task["maxTimes"]
						self.task_duration["isFinished"] = task["isFinished"]
						self.task_duration["finishedTimes"] = task["finishedTimes"]
						self.task_duration["times"] = task["rules"]["maxTotalTime"]
						# next_rolls = task["rules"]["maxTotalTime"][self.task_official["finishedTimes"]]
			if self.task_news["code"] == 0 and self.task_duration["code"] == 0 and self.low_read_task["code"] == 0:
				times = int(self.task_duration["finishedTimes"])
				for i in range(times, len(self.task_duration["times"])):
					if int(self.task_duration["times"][i])%int(self.task_news["readTime"]) == 0:
						self.task_duration["times"][i] = int(self.task_duration["times"][i])/int(self.task_news["readTime"])
					else:
						self.task_duration["times"][i] = int(self.task_duration["times"][i])/int(self.task_news["readTime"]) +1
					self.task_duration["times"][i] = int(self.task_duration["times"][i]) - int(self.task_news["finishedTimes"]) - int(self.low_read_task["finishedTimes"])

				self.task_duration["times"] = self.task_duration["times"][times:]
				


if __name__ == "__main__":
	a = task()
	a.get_tasklist()
	print (a.task_duration)